Skip to content

Fix GH-25880: HTTP wrapper resolves "//host/path" Location against the original host (master) - #25896

Open
matyapiro31 wants to merge 1 commit into
php:masterfrom
matyapiro31:fix/http-wrapper-network-path-location-master
Open

matyapiro31 wants to merge 1 commit into
php:masterfrom
matyapiro31:fix/http-wrapper-network-path-location-master

Conversation

@matyapiro31

Copy link
Copy Markdown
Contributor

Fixes GH-25880.

Replaces #25881, which targeted PHP-8.4 — per @devnexen's comment this qualifies more for master, since it changes where a redirect goes.

Problem

A redirect Location value starting with // is a network-path reference (RFC 3986 section 4.2): it replaces the authority and path and keeps the scheme. The HTTP wrapper instead treated it as an absolute path on the original host.

Before (Location: //other.example/target):

sequenceDiagram
    participant P as PHP (http wrapper)
    participant A as original.example
    participant B as other.example
    P->>A: GET /a/b
    A-->>P: 302 Location: //other.example/target
    Note over P: "/" prefix => treated as absolute path<br/>URL = http://original.example//other.example/target
    P->>A: GET //other.example/target
    Note over B: never contacted
Loading

After:

sequenceDiagram
    participant P as PHP (http wrapper)
    participant A as original.example
    participant B as other.example
    P->>A: GET /a/b
    A-->>P: 302 Location: //other.example/target
    Note over P: "//" prefix => network-path reference<br/>URL = http: + //other.example/target
    P->>B: GET /target
    Note over P: host differs => same_origin = false<br/>=> credentials are stripped
Loading

Change

In the redirect handling of ext/standard/http_fopen_wrapper.c, a Location beginning with // now becomes <current scheme>:<Location> and goes through the existing path (php_uri_parse_to_struct, same_origin check, control character check). Nothing else in the resolution logic changed.

flowchart TD
    L[Location header] --> Q{starts with '//'?}
    Q -- yes, new --> N["new_path = scheme + ':' + Location"]
    Q -- no --> S{absolute URL?<br/>http/https/ftp/ftps}
    S -- yes --> U[use as is]
    S -- no --> R["relative / absolute-path:<br/>unchanged behavior"]
    N --> P[php_uri_parse_to_struct + same_origin check]
    U --> P
    R --> P
Loading

This is the same fix as #25881, re-applied on top of master's refactored redirect-resolution code (the in-place-decode removal and the control-character-check helper added since PHP-8.4, and the case-insensitive scheme check from GHSA-f6gg-rchx-xw8j).

Tests

New http_network_path_location_redirect.phpt: fails before this change (request target is //ADDR/target), passes after (/target).

Notes

I could not locally build this checkout of master end-to-end to run the test myself — an unrelated Windows build-system issue (Zend/zend_portability.h now requires C11, which this machine's MSVC configuration doesn't enable) blocks nmake before it reaches this code. The logic itself is unchanged from the already-reviewed #25881; relying on CI here.

  • Other relative forms (?query-only, dot segments) are intentionally left unchanged.

🤖 Generated with Claude Code

… the original host

A Location value starting with "//" is a network-path reference (RFC 3986
section 4.2) that replaces authority and path while keeping the scheme.
The redirect code only distinguished absolute URLs from values starting
with a single "/", so "//other.example/x" was sent as the request target
"//other.example/x" to the original host instead of following the redirect
to other.example.

Treat it as a network-path reference and prefix the current scheme. The
resulting URL goes through the existing origin check, so credentials are
still stripped when the host differs.

Adapted from the PHP-8.4 fix in php#25881 to master's refactored redirect
resolution code (in-place-decode removal, GHSA-f6gg-rchx-xw8j scheme check).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTP wrapper: Incorrect redirect resolution of relative path in Location

1 participant